{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 265,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入需要使用的库：\n",
    "import pandas as pd\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"../tmp/data_preprocessing.csv\")\n",
    "\n",
    "# 处理数据集，将红酒的品质作为标签，其余特征作为特征：\n",
    "X = df.drop(['quality'], axis=1)\n",
    "y = df['quality']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据归一化处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 又因为前面数据质量分析画的图中又许多的偏离正常值的点，且前面也分析了数据大概率是不符合正态分布的\n",
    "# 所以这里选择数据归一化处理\n",
    "# 直接使用使用 Scikit-learn 库进行数据归一化处理\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "scaler = MinMaxScaler()\n",
    "# 用数据对 MinMaxScaler 拟合\n",
    "scaler.fit(X)\n",
    "# 对数据进行归一化处理：\n",
    "X = scaler.transform(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 随机森林模型\n",
    "- 为什么选择随机森林模型呢？ \n",
    "\n",
    "1、准确率高：\n",
    "随机森林模型是基于决策树的集成模型，在大量决策树组合的基础上，通过“随机抽样”和“随机选择特征”等方法进行优化，大大提高了模型的准确率。\n",
    "\n",
    "2、我们的数据有许多的异常值，而随机森林模型具有良好的鲁棒性，能够有效抵抗噪声和异常点等干扰因素。\n",
    "\n",
    "3、适应性强：随机森林模型适用于不同规模、不同形态的数据集，并且可以应对多种分类和回归问题。\n",
    "\n",
    "4、不易过拟合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 282,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率: 63.97%\n",
      "精确率: 32.08%\n",
      "召回率: 32.51%\n",
      "F1分数: 32.18%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\ysc\\anaconda3\\Anaconda3 2022.10\\lib\\site-packages\\sklearn\\metrics\\_classification.py:1318: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
      "  _warn_prf(average, modifier, msg_start, len(result))\n"
     ]
    }
   ],
   "source": [
    "# 随机分割分割数据集为训练集和测试集：\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "# 建立随机森林模型：\n",
    "rfc = RandomForestClassifier(n_estimators=100, random_state=6)\n",
    "# 其中，n_estimators 表示决策树的数量，\n",
    "# random_state 表示随机数生成器的种子值，保证每次运行结果一致。\n",
    "\n",
    "# 在训练集上进行拟合和训练：\n",
    "rfc.fit(X_train, y_train)\n",
    "# 在测试集上进行预测：\n",
    "y_pred = rfc.predict(X_test)\n",
    "\n",
    "# 评估模型指标\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "precision = precision_score(y_test, y_pred, average='macro')\n",
    "recall = recall_score(y_test, y_pred, average='macro')\n",
    "f1 = f1_score(y_test, y_pred, average='macro')\n",
    "\n",
    "# 打印模型的评估指标\n",
    "print(\"准确率: {:.2f}%\".format(accuracy*100))\n",
    "print(\"精确率: {:.2f}%\".format(precision*100))\n",
    "print(\"召回率: {:.2f}%\".format(recall*100)) # \n",
    "print(\"F1分数: {:.2f}%\".format(f1*100)) # 这可能意味着模型不能够准确地识别正类别，同时也会出现将正分类误判为负分类，所以模型不太好！\n",
    "# 准确率是评价预测模型常用的指标：\n",
    "# 从各项指标来看，此模型其实并不是很好,我们可以对模型进行优化以下(使用交叉验证进行优化)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征集： (1357, 11)\n",
      "标签集： (1357,)\n",
      "交叉验证准确度: 0.568 +/- 0.036\n"
     ]
    }
   ],
   "source": [
    "# 所有数据做交叉验证\n",
    "from sklearn.linear_model import LassoCV\n",
    "from sklearn.model_selection import cross_val_score\n",
    "import numpy as np\n",
    "scores = cross_val_score(rfc, X, y, cv=10)\n",
    "print('交叉验证准确度: %.3f +/- %.3f' % (np.mean(scores), np.std(scores)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.13 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "79c2bc36f36ebca92fe5f3c73355a3866f3a34a4fd0b1d43087d5313dd5fd5ff"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
